package com.webchat.chat.service.impl;

import com.webchat.chat.core.enums.ServiceStatus;
import com.webchat.chat.core.enums.SocketClientType;
import com.webchat.chat.core.enums.SocketRoomStatus;
import com.webchat.chat.core.enums.UserOnlineStatus;
import com.webchat.chat.dao.RoomClientMapper;
import com.webchat.chat.dao.SocketClientMapper;
import com.webchat.chat.dao.SocketRoomMapper;
import com.webchat.chat.dao.UserMapper;
import com.webchat.chat.model.RoomClient;
import com.webchat.chat.model.SocketClient;
import com.webchat.chat.model.SocketRoom;
import com.webchat.chat.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 服务器启动检测数据库，是否有正常会话，有就修改相应状态
 * 服务器重启时或异常，导致会话正常关闭事件没触发，数据库异常
 *
 */
@Service
@Transactional
public class RefreshChatStatusService {

    private static final Logger logger = LoggerFactory.getLogger(RefreshChatStatusService.class);

    @Resource
    private RoomClientMapper roomClientMapper;

    @Resource
    private SocketClientMapper socketClientMapper;

    @Resource
    private SocketRoomMapper socketRoomMapper;

    @Resource
    private UserMapper userMapper;

    public void refreshChatStatus() {
        long s = System.currentTimeMillis();
        refreshRoomClient();
        refreshSocketRoom();
        refreshSocketClient();
        refreshUsetOnlineStatus();
        long e = System.currentTimeMillis();
        double t = (e-s)/1000;
        logger.info("服务器启动刷新执行时间:{}", t);
    }

    private void refreshUsetOnlineStatus() {
        User user = new User();
        user.setOnlineStatus(UserOnlineStatus.在线);
        List<User> userList = userMapper.select(user);
        if (null != userList && !userList.isEmpty()) {
            for (User userDB : userList) {
                user.setOnlineStatus(UserOnlineStatus.离线);
                userMapper.updateByPrimaryKey(userDB);
            }
        }
    }

    private void refreshSocketClient() {
        SocketClient socketClient = new SocketClient();
        socketClient.setServiceStatus(ServiceStatus.正常);
        socketClient.setConnected(true);
        List<SocketClient> socketClients = socketClientMapper.selectByStatus(socketClient);

        if (null != socketClients && !socketClients.isEmpty()) {
            for (SocketClient socketC : socketClients) {
                socketC.setServiceStatus(ServiceStatus.离开);
                socketC.setConnected(false);
                socketC.setConnectCustomerNum(0);
                socketClientMapper.updateByPrimaryKey(socketC);
            }
        }
    }

    private void refreshSocketRoom() {
        SocketRoom socketRoom = new SocketRoom();
        socketRoom.setStatus(SocketRoomStatus.正常);
        socketRoom.setType(SocketClientType.客户);
        List<SocketRoom> socketRoomList = socketRoomMapper.select(socketRoom);
        if (null != socketRoomList && !socketRoomList.isEmpty()) {
            for (SocketRoom sockRoom : socketRoomList) {
                sockRoom.setStatus(SocketRoomStatus.断开);
                socketRoomMapper.updateByPrimaryKey(sockRoom);
            }
        }
    }

    private void refreshRoomClient() {
        RoomClient roomClient = new RoomClient();
        roomClient.setStatus(SocketRoomStatus.正常);
        List<RoomClient> roomClientList = roomClientMapper.select(roomClient);
        if (null != roomClientList && !roomClientList.isEmpty()) {
            for (RoomClient roomC : roomClientList) {
                roomC.setStatus(SocketRoomStatus.断开);
                roomClientMapper.updateByPrimaryKey(roomC);
            }
        }
    }
}
